home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3139 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: slsyd1p17.ozemail.com.au!user
  2. From: conor@cognet.com.au (Conor MacNeill)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: char vs. unsigned char  ???
  5. Date: Tue, 23 Jan 1996 00:32:50 +1100
  6. Organization: Cognet Pty Limited
  7. Message-ID: <conor-2301960032500001@slsyd1p17.ozemail.com.au>
  8. References: <DLGEKq.1zq@avalon.chinalake.navy.mil>
  9. NNTP-Posting-Host: slsyd1p17.ozemail.com.au
  10. X-Newsreader: Yet Another NewsWatcher 2.1.2
  11.  
  12. In article <DLGEKq.1zq@avalon.chinalake.navy.mil>, sean harvey
  13. <charles_harvey@imdgw.chinalake.navy.mil> wrote:
  14.  
  15. >I recently encorporated some functions from another program compiled and 
  16. >linked under an older version (Microsoft C version 6.0) into my program 
  17. >(MSVC 7.0).
  18. >
  19. >The alien code would not compile for me due, I guess to more rigorous 
  20. >type-checking.
  21. >
  22. >He used a lot of unsigned char strings that generated compile errors on 
  23. >calls to functions strlen, strcat and sprintf.
  24. >
  25. >So I changed his types to char and compiled cleanly but the program 
  26. >crashes and I think it may be due to the changes.
  27. >
  28. >The functions pass the data around and compress it to 6-bit ascii, among 
  29. >other things.  Then I send it out via a modem.
  30. >
  31. >Somebody splain to me about unsigned char vs char please.  Or any other 
  32. >input.
  33.  
  34. Both char and unsigned char are 8 bits in most implementations. The difference
  35. is in how to interpret those bits.
  36.  
  37. An unsigned char can take the value 0 to 255
  38.  
  39. A signed char can take the values -128 to 127.
  40.  
  41. so the binary value 11111111 is -1 in a signed char but 255 in an 
  42. unsigned char (same bits - different values)
  43.  
  44. Some compilers treat chars as signed, some treat them as unsigned and 
  45. some compilers are configurable.
  46.  
  47. An important difference between signed and unsigned chars occurs 
  48. when they are converted to larger integral types such as ints and longs
  49. Signed characters are sign-extended to become larger types while 
  50. unsigned chars are not.
  51.  
  52. A problem may be occuring if your alien code is using the character value
  53. to index into an array. A unsigned char will work nicely while a signed char
  54. may attempt to access memory outside the array (since the index may become 
  55. negative). Without more info, this is a wild guess at your problem :-)
  56.  
  57. Good Luck
  58.  
  59. -- 
  60. Conor MacNeill
  61. conor@cognet.com.au
  62. Cognet Pty Limited
  63.